
package com.cmc.dao.hibernate;

import java.util.List;

import com.cmc.dao.hibernate.BaseDaoHibernate;
import com.cmc.model.SysAnnounce;
import com.cmc.dao.SysAnnounceDao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;

public class SysAnnounceDaoHibernate extends BaseDaoHibernate implements SysAnnounceDao {

    /**
     * @see com.cmc.dao.SysAnnounceDao#getSysAnnounces(com.cmc.model.SysAnnounce)
     */
    public List getSysAnnounces(final SysAnnounce sysAnnounce) {
        //return getHibernateTemplate().find("from SysAnnounce");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.*/
        if (sysAnnounce == null) {
            return getHibernateTemplate().find("from SysAnnounce");
        } else {
            // filter on properties set in the sysAnnounce
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(sysAnnounce).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(SysAnnounce.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }
    }

    /**
     * @see com.cmc.dao.SysAnnounceDao#getSysAnnounce(Long id)
     */
    public SysAnnounce getSysAnnounce(final Long id) {
        SysAnnounce sysAnnounce = (SysAnnounce) getHibernateTemplate().get(SysAnnounce.class, id);
        if (sysAnnounce == null) {
            log.warn("uh oh, sysAnnounce with id '" + id + "' not found...");
            throw new ObjectRetrievalFailureException(SysAnnounce.class, id);
        }

        return sysAnnounce;
    }

    /**
     * @see com.cmc.dao.SysAnnounceDao#saveSysAnnounce(SysAnnounce sysAnnounce)
     */    
    public void saveSysAnnounce(final SysAnnounce sysAnnounce) {
        getHibernateTemplate().saveOrUpdate(sysAnnounce);
    }

    /**
     * @see com.cmc.dao.SysAnnounceDao#removeSysAnnounce(Long id)
     */
    public void removeSysAnnounce(final Long id) {
        getHibernateTemplate().delete(getSysAnnounce(id));
    }
}
